import re
from datetime import datetime
import locale

from ferenda import util
from ferenda import Describer, DocumentRepository


class W3Standards(DocumentRepository):
    alias = "w3c"
    start_url = "http://www.w3.org/TR/tr-status-all"
    document_url_regex = "http://www.w3.org/TR/(?P<year>\d{4})/REC-(?P<basefile>.*)-(?P<date>\d+)"
    document_url_template = None # no simple way of creating a url
                                 # from a basefile alone (we also need
                                 # the published date)
    basefile_regex = None # Link text on index page do not contain basefile
    parse_content_selector="body"
    parse_filter_selectors=["div.toc", "div.head"]

    def parse_metadata_from_soup(self, soup, doc):
        d = Describer(doc.meta, doc.uri)
        dct = self.ns['dct']

        # dct:title
        d.value(dct.title,soup.find("title").string, lang=doc.lang)
        d.value(dct.identifier,doc.basefile)
        # dct:abstract
        abstract = soup.find(_class="abstract")
        if abstract:
            d.value(dct['abstract'],abstract.string, lang=doc.lang)

        # dct:published
        datehdr = soup.find(lambda x: x.name in ('h2','h3')
                            and re.search("W3C\s+Recommendation,?\s+",x.text))
        # from pudb import set_trace; set_trace()
        if datehdr:
            datestr = " ".join(datehdr.text.split())
            m = re.search("(\d+) (\w+),? (\d{4})", datestr)
            if not m:
                self.log.warning("%s: Couldn't parse datestr %s" %
                                 (doc.basefile, datestr))
            else:
                datestr = " ".join(m.groups())
                date = None
                # FIXME: should this contrived workaround to get default
                # (english) locale for strptime be put in a util class?
                l = locale.getlocale(locale.LC_ALL)
                locale.setlocale(locale.LC_ALL,'C')
                try:
                     # 17 December 1996
                    date = datetime.strptime(datestr,"%d %B %Y")
                except ValueError:
                    try: 
                        # 17 Dec 1996
                        date = datetime.strptime(datestr,"%d %b %Y")
                    except ValueError:
                        self.log.warning("%s: Could not parse datestr %s" %
                                         (doc.basefile, datestr))
                finally:
                    locale.setlocale(locale.LC_ALL,l)
                if date:
                    d.value(dct.issued, date)

        # dct:editor
        editors = soup.find("dt", text=re.compile("Editors?:"))
        if editors:
            for editor in editors.find_next_siblings("dd"):
                editor_string = " ".join(editor.stripped_strings)
                editor_name = editor_string.split(", ")[0]
                d.value(dct.editor, editor_name)

        # assure we got exactly one of each of the required properties
        for required in (dct.title,dct.issued):
            d.getvalue(required) # throws KeyError if not found (or more than one)
